﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _3__._4
{
    public partial class Form1 : Form
    {
        // Vehicle基类（参考例3.1）
        class Vehicle
        {
            protected int wheels;       // 车轮数
            protected float weight;     // 车重量

            // 基类的SetVehicle方法
            public void SetVehicle(int wheels, float weight)
            {
                this.wheels = wheels;
                this.weight = weight;
            }

            // 基类的GetVehicle方法（虚方法，用于演示重写）
            public virtual void GetVehicle()
            {
                MessageBox.Show($"车轮数：{wheels}\n车重量：{weight}");
            }
        }

        // Truck派生类（继承自Vehicle）
        class Truck : Vehicle
        {
            private float maxLoad;      // 最大载重量
            private float oilPerKm;     // 每公里耗油量

            // 重载SetVehicle方法（参数个数与基类不同）
            public void SetVehicle(int wheels, float weight, float maxLoad, float oilPerKm)
            {
                this.wheels = wheels;
                this.weight = weight;
                this.maxLoad = maxLoad;
                this.oilPerKm = oilPerKm;
            }

            // 判断是否超载的方法
            public bool Overload(float nowLoad)
            {
                return nowLoad > maxLoad;
            }

            // 计算总耗油量的方法
            public float OilConsumption(float runKm)
            {
                return runKm * oilPerKm;
            }

            // 重写GetVehicle方法（用new关键字隐藏基类方法）
            public new void GetVehicle()
            {
                MessageBox.Show($"车轮数：{wheels}\n车重量：{weight}\n载重量：{maxLoad}\n每公里耗油：{oilPerKm}");
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        
            private void button1_Click(object sender, EventArgs e)
        {
            // 从TextBox中读取输入并转换类型
            int wheels = int.Parse(textBox1.Text);
            float weight = float.Parse(textBox2.Text);
            float maxLoad = float.Parse(textBox3.Text);
            float oilPerKm = float.Parse(textBox4.Text);
            float nowLoad = float.Parse(textBox5.Text);
            float runKm = float.Parse(textBox6.Text);

            // 创建Truck对象并设置属性
            Truck t = new Truck();
            t.SetVehicle(wheels, weight, maxLoad, oilPerKm);

            // 判断是否超载并执行对应逻辑
            if (t.Overload(nowLoad))
            {
                MessageBox.Show("对不起！卡车已超重，请卸下一些货物。");
                textBox5.Text = "";  // 清空载货量输入框
                textBox5.Focus();   // 聚焦到载货量输入框
            }
            else
            {
                float totalOil = t.OilConsumption(runKm);
                MessageBox.Show($"卡车正在行驶中…\n现已耗油{totalOil}升！");
                t.GetVehicle();  // 调用重写的GetVehicle方法，显示卡车详细信息
            }
        }
    }
    }

